import {
  find
} from '../../../api/order';
import {
  returns,
  calculate
} from '../../../api/askfor';
import {
  add
} from '../../../api/file';
const util = require('../../../utils/util.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose: [{
        title: '拍多/拍错/不想要'
      },
      {
        title: '信息填写错误，重新拍'
      },
      {
        title: '支付方式错误'
      },
      {
        title: '商品发货太慢'
      },
      {
        title: '其他原因'
      },
    ],
    type:'',
    whyIndex: 0,
    whyName: '请选择',
    whyType: false,
    previewImgs: [],
    images: [],
    orderForm: null,
    id: '',
    returnsForm: {
      amountPayable: 0,
      subPrice: 0
    },
    metaForm: null,
    memo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  
    this.data.type=options.type
    console.log(this.data.type)
    this.findClick(options.id)
    this.data.id = options.id
  },
  chooseClick(data) {
    var _this = this
    console.log(data)
    var index = data.currentTarget.dataset.index
    if (_this.data.orderForm.itemList[index].availableReturnQuantity == 0){
      console.log('111')
      return
    }
    if (_this.data.orderForm.itemList[index].returnQuantity == 0) {
      _this.data.orderForm.itemList[index].returnQuantity = _this.data.orderForm.itemList[index].availableReturnQuantity;
    } else if (_this.data.orderForm.itemList[index].returnQuantity != 0) {
      _this.data.orderForm.itemList[index].returnQuantity = 0;
    }
    _this.calculateClick()
    _this.setData({
      orderForm: _this.data.orderForm
    })
    console.log(_this.data.orderForm)
  },
  chooseWhy(data) {
    console.log(data)
    this.data.whyIndex = data.currentTarget.dataset.index
    this.setData({
      whyIndex: this.data.whyIndex
    })
  },
  cancelClick() {
    this.data.whyType = false
    this.setData({
      whyType: this.data.whyType
    })
  },
  determineClick() {
    this.data.whyType = false
    this.data.whyName = this.data.choose[this.data.whyIndex].title
    this.setData({
      whyType: this.data.whyType,
      whyName: this.data.whyName
    })
  },
  chooseBotton() {
    this.data.whyType = true
    this.setData({
      whyType: this.data.whyType
    })
  },
  submitClick() {
    var _this = this
    var itemList = []
    _this.data.orderForm.itemList.forEach(function (item) {
      if (item.returnQuantity != 0) {
        itemList.push(item)
      }
    })
    if (itemList.length == 0) {
      wx.showToast({
              icon: "none",
        title: '请选择退款商品',
      })
      return
    }
    if (_this.data.whyName == '请选择') {
      wx.showToast({
              icon: "none",
        title: '请选择退款原因',
      })
      return
    }
    wx.showModal({
      title: '福利有效期风险提醒',
      content: '有效期临近，该部分退款如在有效期后到账，将会自动过期。',
      success(res) {
        if (res.confirm) {
          _this.returnsClick()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  findClick(data) {
    var _this = this
    console.log(data)
    find(data).then(
      res => {
        if (res.type == 'success') {
          var list=[]
          if(this.data.type==2){
            res.data.order.itemList.forEach(item=>{
              if(item.subType==3){
                list.push(item)
              }
            })
           res.data.order.itemList=list
          }
          _this.data.orderForm = res.data.order
          _this.data.orderForm.itemList.forEach(function (item) {
             item.availableReturnQuantity = item.quantity - item.returnQuantity
            if (item.returnQuantity !=0){
              item.returnQuantity =0
            }
          })
          
          _this.setData({
            orderForm: _this.data.orderForm
          })
          console.log(_this.data.orderForm)
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
  calculateClick() {
    var _this = this
    var itemList = []
    _this.data.orderForm.itemList.forEach(function(item) {
      if (item.returnQuantity != 0) {
        itemList.push(item)
      }
    })
    calculate(itemList, _this.data.id).then(
      res => {
        if (res.type == 'success') {
          console.log(res)
          _this.data.returnsForm = res.data
          _this.setData({
            returnsForm: _this.data.returnsForm
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },

  returnsClick() {
    var _this = this
    var itemList = []
    _this.data.orderForm.itemList.forEach(function(item) {
      if (item.returnQuantity != 0) {
        itemList.push(item)
      }
    })
    // if (itemList.length == 0) {
    //   wx.showToast({
        //   icon: "none",
    //     title: '请选择退款商品',
    //   })
    //   return
    // }
    // if (_this.data.whyName == '请选择') {
    //   wx.showToast({
                //   icon: "none",
    //     title: '请选择退款原因',
    //   })
    //   return
    // }
    var memoData = {
      reason: _this.data.whyName,
      memo: _this.data.memo
    }
    returns(itemList,memoData,_this.data.id).then(
      res => {
        if (res.type == 'success') {
          _this.fileAdd(res.data.id)
          wx.redirectTo({
            url: '/pages/member/order/refund_details?id='+res.data.id,
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
  metaClick(data) {
    console.log(data)
    this.data.metaForm = data.detail
  },
  memoClick(data) {
    var _this = this
    console.log(data)
    _this.data.memo = data.detail.value
    _this.setData({
      memo: _this.data.memo
    })
  },
  fileAdd(data) {
    var _this = this
    var metaForm = []
    if (!util.isNull(_this.data.metaForm) && !util.isNull(_this.data.metaForm.images != [])){
      _this.data.metaForm.images.forEach(function (item) {
        var meta = {
          fileName: 'fileImg',
          fileType: 0,
          fileUrl: item,
          orderType: 'askfor',
          orderId: data
        }
        metaForm.push(meta)
      })
    }
    if (!util.isNull(_this.data.metaForm) && !util.isNull(_this.data.metaForm.video != [])){
      _this.data.metaForm.video.forEach(function (item) {
        var meta = {
          fileName: 'fileVideo',
          fileType: 0,
          fileUrl: item,
          orderType: 'askfor',
          orderId: data
        }
        metaForm.push(meta)
      })
    }
    add(metaForm).then(
      res => {
        if (res.type == 'success') {
           console.log(res)
        }
      }
    ).catch(error => {
      wx.showToast({
              icon: "none",
        title: error.content,
      })
    })
  }
})